<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 6.0.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/logo.ico">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/logo.ico">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.4/css/all.min.css" integrity="sha256-mUZM63G8m73Mcidfrv5E+Y61y7a12O5mW4ezU3bxqW4=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"example.com","root":"/","images":"/images","scheme":"Mist","darkmode":false,"version":"8.10.0","exturl":false,"sidebar":{"position":"right","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

  <meta name="description" content="调试前首先要gcc -g，在可执行文件中生成调试信息。实际的项目代码往往是用cmake编译的。通过控制CMAKE_BUILD_TYPE可以选择是否生成调试信息。例如 12set(CMAKE_BUILD_TYPE Debug)set(CMAKE_BUILD_TYPE RelWithDebInfo)">
<meta property="og:type" content="article">
<meta property="og:title" content="GDB基础">
<meta property="og:url" content="http://example.com/2017/08/19/gdb/index.html">
<meta property="og:site_name" content="chenchu.zs&#39;s blog">
<meta property="og:description" content="调试前首先要gcc -g，在可执行文件中生成调试信息。实际的项目代码往往是用cmake编译的。通过控制CMAKE_BUILD_TYPE可以选择是否生成调试信息。例如 12set(CMAKE_BUILD_TYPE Debug)set(CMAKE_BUILD_TYPE RelWithDebInfo)">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2017-08-18T16:00:00.000Z">
<meta property="article:modified_time" content="2022-06-08T12:02:03.432Z">
<meta property="article:author" content="Zhang Shu">
<meta property="article:tag" content="工具使用">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="http://example.com/2017/08/19/gdb/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"http://example.com/2017/08/19/gdb/","path":"2017/08/19/gdb/","title":"GDB基础"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>GDB基础 | chenchu.zs's blog</title>
  





  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">chenchu.zs's blog</p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">仓鼠君的瓜子库</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu">
        <li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li>
        <li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a></li>
        <li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a></li>
        <li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#GDB%E5%9F%BA%E7%A1%80%E5%91%BD%E4%BB%A4"><span class="nav-number">1.</span> <span class="nav-text">GDB基础命令</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%89%93%E6%96%AD%E7%82%B9-break-x2F-b"><span class="nav-number">1.1.</span> <span class="nav-text">打断点: break&#x2F;b</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E8%AE%BE%E7%BD%AE%E8%BF%90%E8%A1%8C%E5%8F%82%E6%95%B0"><span class="nav-number">1.2.</span> <span class="nav-text">设置运行参数</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E8%BF%90%E8%A1%8C%E7%A8%8B%E5%BA%8F"><span class="nav-number">1.3.</span> <span class="nav-text">运行程序</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%BB%A7%E7%BB%AD%E6%89%A7%E8%A1%8C"><span class="nav-number">1.4.</span> <span class="nav-text">继续执行</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%A3%80%E6%9F%A5%E5%8F%98%E9%87%8F-print-x2F-watch"><span class="nav-number">1.5.</span> <span class="nav-text">检查变量: print&#x2F;watch</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%88%97%E5%87%BA%E7%A8%8B%E5%BA%8F%E6%BA%90%E7%A0%81-l%EF%BC%88list%EF%BC%89"><span class="nav-number">1.6.</span> <span class="nav-text">列出程序源码  l（list）</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%A0%88%E8%B7%9F%E8%B8%AA-bt%EF%BC%88backtrace%EF%BC%9A%E5%9B%9E%E6%BA%AF%EF%BC%89"><span class="nav-number">1.7.</span> <span class="nav-text">栈跟踪  bt（backtrace：回溯）</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%9F%A5%E7%9C%8B%E5%86%85%E5%AD%98"><span class="nav-number">1.8.</span> <span class="nav-text">查看内存</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#gdb%E7%9A%84%E5%85%B6%E4%BB%96%E6%8A%80%E5%B7%A7"><span class="nav-number">2.</span> <span class="nav-text">gdb的其他技巧</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#gdb%E6%9F%A5%E7%9C%8B%E6%B1%87%E7%BC%96%E4%BB%A3%E7%A0%81"><span class="nav-number">2.1.</span> <span class="nav-text">gdb查看汇编代码</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#gdb-attach"><span class="nav-number">2.2.</span> <span class="nav-text">gdb attach</span></a></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Zhang Shu"
      src="/images/avatar.gif">
  <p class="site-author-name" itemprop="name">Zhang Shu</p>
  <div class="site-description" itemprop="description">积跬步以致千里</div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">13</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">6</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">1</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author site-overview-item animated">
      <span class="links-of-author-item">
        <a href="https://github.com/chenchu-zs" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;chenchu-zs" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
  </div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://example.com/2017/08/19/gdb/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="Zhang Shu">
      <meta itemprop="description" content="积跬步以致千里">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="chenchu.zs's blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          GDB基础
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2017-08-19 00:00:00" itemprop="dateCreated datePublished" datetime="2017-08-19T00:00:00+08:00">2017-08-19</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar-check"></i>
      </span>
      <span class="post-meta-item-text">更新于</span>
      <time title="修改时间：2022-06-08 20:02:03" itemprop="dateModified" datetime="2022-06-08T20:02:03+08:00">2022-06-08</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/c/" itemprop="url" rel="index"><span itemprop="name">c++</span></a>
        </span>
    </span>

  
    <span class="post-meta-break"></span>
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>1.1k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>1 分钟</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <p>调试前首先要<strong>gcc -g</strong>，在可执行文件中生成调试信息。<br>实际的项目代码往往是用cmake编译的。通过控制CMAKE_BUILD_TYPE可以选择是否生成调试信息。例如</p>
<figure class="highlight cmake"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">set</span>(CMAKE_BUILD_TYPE Debug)</span><br><span class="line"><span class="keyword">set</span>(CMAKE_BUILD_TYPE RelWithDebInfo)</span><br></pre></td></tr></table></figure>
<span id="more"></span>
<h1 id="GDB基础命令"><a href="#GDB基础命令" class="headerlink" title="GDB基础命令"></a>GDB基础命令</h1><h2 id="打断点-break-x2F-b"><a href="#打断点-break-x2F-b" class="headerlink" title="打断点: break&#x2F;b"></a>打断点: break&#x2F;b</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">b &lt;函数名&gt;/&lt;行数&gt;</span><br></pre></td></tr></table></figure>
<ul>
<li>b main:在main函数处打断点。</li>
<li>b 8 ：在第8行打断点</li>
<li>info b：查看所有断点</li>
<li>d xxx ：删除断点（delete）</li>
<li>disable x：禁用断点（但是仍然存在）</li>
<li>enable x：激活断点</li>
</ul>
<h2 id="设置运行参数"><a href="#设置运行参数" class="headerlink" title="设置运行参数"></a>设置运行参数</h2><p>在程序执行之前就应该先设置好参数</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">set args &lt;参数列表&gt;</span><br></pre></td></tr></table></figure>
<h2 id="运行程序"><a href="#运行程序" class="headerlink" title="运行程序"></a>运行程序</h2><ul>
<li><code>r（run）</code>（相当于vs里的F5，开始执行直到遇到断点或者退出）</li>
<li><code>start</code> :（相当于vs里的F10，开始调试，单步执行）</li>
</ul>
<h2 id="继续执行"><a href="#继续执行" class="headerlink" title="继续执行"></a>继续执行</h2><ul>
<li><code>n（next）</code>：向下单步执行，不进入函数。相当于vs里的F10</li>
<li><code>s（step）</code>：向下单步执行，但是进入函数。相当于vs的F11</li>
<li><code>finish</code>：运行完当前函数，跳出后停止。</li>
<li><code>c（continue</code>：向下执行到下一个断点。（相当于vs里不单步，继续F5）</li>
</ul>
<h2 id="检查变量-print-x2F-watch"><a href="#检查变量-print-x2F-watch" class="headerlink" title="检查变量: print&#x2F;watch"></a>检查变量: print&#x2F;watch</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">p &lt;待检测的变量&gt;</span><br><span class="line">watch &lt;变量名&gt; 监视变量（当发生变动时自动打印）</span><br></pre></td></tr></table></figure>
<ul>
<li><code>p a</code> ：查看变量a的值</li>
<li><code>p &amp;b</code>：查看变量b的地址</li>
<li><code>p c[1]</code>: 查看数组c的下标为1的值</li>
</ul>
<h2 id="列出程序源码-l（list）"><a href="#列出程序源码-l（list）" class="headerlink" title="列出程序源码  l（list）"></a>列出程序源码  l（list）</h2><h2 id="栈跟踪-bt（backtrace：回溯）"><a href="#栈跟踪-bt（backtrace：回溯）" class="headerlink" title="栈跟踪  bt（backtrace：回溯）"></a>栈跟踪  bt（backtrace：回溯）</h2><p>bt用来定位崩溃位置。<strong>这是gdb最常见的应用，实际工作中其他错误都可以通过打印日志的方式来确定。</strong></p>
<h2 id="查看内存"><a href="#查看内存" class="headerlink" title="查看内存"></a>查看内存</h2><p><strong>查看内存是GDB另一个非常重要的应用</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">x /&lt;nfu&gt;</span><br></pre></td></tr></table></figure>
<ul>
<li>x 是 examine 的缩写</li>
<li>n表示要显示的内存单元的个数</li>
<li>f表示显示方式, 可取如下值<ul>
<li>x 按十六进制格式显示变量。</li>
<li>d 按十进制格式显示变量。</li>
<li>u 按十进制格式显示无符号整型。</li>
<li>o 按八进制格式显示变量。</li>
<li>t 按二进制格式显示变量。</li>
<li>a 按十六进制格式显示变量。</li>
<li>i 指令地址格式</li>
<li>c 按字符格式显示变量。</li>
<li>f 按浮点数格式显示变量。</li>
</ul>
</li>
<li>u表示一个地址单元的长度<ul>
<li>b表示单字节，</li>
<li>h表示双字节，</li>
<li>w表示四字节，</li>
<li>g表示八字节</li>
</ul>
</li>
</ul>
<p>例如 x &#x2F;20xb  </p>
<h1 id="gdb的其他技巧"><a href="#gdb的其他技巧" class="headerlink" title="gdb的其他技巧"></a>gdb的其他技巧</h1><ul>
<li><code>info locals</code>：可以查看当前函数的所有局部变量</li>
<li><code>info registers esp ebp</code>：可以查看寄存器值</li>
</ul>
<h2 id="gdb查看汇编代码"><a href="#gdb查看汇编代码" class="headerlink" title="gdb查看汇编代码"></a>gdb查看汇编代码</h2><ul>
<li><code>ni</code> 在汇编中单步，不进入函数内部</li>
<li><code>si</code> 在汇编中单步，要进入函数内部</li>
<li><code>set disassemble-next-line on</code> 断点时查看行号</li>
</ul>
<h2 id="gdb-attach"><a href="#gdb-attach" class="headerlink" title="gdb attach"></a>gdb attach</h2><p>attach到进程上然后调试</p>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/%E5%B7%A5%E5%85%B7%E4%BD%BF%E7%94%A8/" rel="tag"># 工具使用</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2017/02/28/union-find/" rel="prev" title="算法笔记——并查集">
                  <i class="fa fa-chevron-left"></i> 算法笔记——并查集
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2021/03/12/docker-env/" rel="next" title="Docker学习笔记——环境配置">
                  Docker学习笔记——环境配置 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Zhang Shu</span>
</div>
<div class="wordcount">
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-line"></i>
    </span>
    <span title="站点总字数">22k</span>
  </span>
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="站点阅读时长">20 分钟</span>
  </span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/mist/" rel="noopener" target="_blank">NexT.Mist</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/schemes/muse.js"></script><script src="/js/next-boot.js"></script>

  
<script src="https://cdn.jsdelivr.net/npm/hexo-generator-searchdb@1.4.0/dist/search.js" integrity="sha256-vXZMYLEqsROAXkEw93GGIvaB2ab+QW6w3+1ahD9nXXA=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>





  




  

  <script class="next-config" data-name="enableMath" type="application/json">true</script><script class="next-config" data-name="mathjax" type="application/json">{"enable":true,"tags":"none","js":{"url":"https://cdn.jsdelivr.net/npm/mathjax@3.2.0/es5/tex-mml-chtml.js","integrity":"sha256-r+3itOMtGGjap0x+10hu6jW/gZCzxHsoKrOd7gyRSGY="}}</script>
<script src="/js/third-party/math/mathjax.js"></script>



</body>
</html>
